package com.suny.sf_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suny.Fence.dto.FenceUpdateStatusDTO;
import com.suny.sf_server.service.FenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.suny.Fence.dto.FenceAddDTO;
import com.suny.Fence.dto.FenceUpdateDTO;
import com.suny.Fence.vo.FenceVO;
import com.suny.exception.BusinessException;
import com.suny.result.PageDTO;
import com.suny.result.PageVO;
import com.suny.result.R;
import com.suny.Fence.dto.FenceQueryDTO;

import java.util.List;

@Slf4j
@Tag(name = "围栏管理", description = "围栏管理相关接口")
@RestController
@RequestMapping("/fence")
public class FenceController {
    @Resource
    private FenceService fenceService;
    
    @Operation(summary = "新增围栏")
    @PostMapping("/add")
    public R add(@Validated @RequestBody FenceAddDTO dto) {
        try {
            fenceService.add(dto);
            return R.OK("新增围栏成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "修改围栏")
    @PostMapping("/update")
    public R update(@Validated @RequestBody FenceUpdateDTO dto) {
        try {
            fenceService.update(dto);
            return R.OK("修改围栏成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "更新围栏状态")
    @PostMapping("/updateStatus")
    public R updateStatus(@Validated @RequestBody FenceUpdateStatusDTO dto) {
        try {
            Integer fenceId = dto.getFenceId();
            Integer status = dto.getStatus();
            fenceService.updateStatus(fenceId, status);
            return R.OK("更新状态成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }

//    @Operation(summary = "获取围栏列表")
//    @PostMapping("/list")
//    public R getList(@Validated @RequestBody PageDTO pageDTO) {
//        log.info("接收到分页查询参数: {}", pageDTO);
//        try {
//            Page<FenceVO> page = fenceService.getList(pageDTO);
//
//            // 构造返回的分页数据
//            PageVO<FenceVO> result = new PageVO<>();
//            result.setCurrent(page.getCurrent());
//            result.setSize(page.getSize());
//            result.setTotal(page.getTotal());
//            result.setPages(page.getPages());
//            result.setRecords(page.getRecords());
//
//            log.info("查询结果: total={}, size={}", page.getTotal(), page.getRecords().size());
//            return R.OK(result);
//        } catch (BusinessException e) {
//            log.error("查询失败", e);
//            return R.FAIL(e.getMessage());
//        }
//    }

    @Operation(summary = "下拉框获取可用围栏")
    @GetMapping("/select")
    public R getEnableFencesByPenId(@Parameter(description = "圈舍ID") Integer penId) {
        log.info("查询圈舍[{}]下的可用围栏", penId);
        try {
            List<FenceVO> list = fenceService.getEnableFencesByPenId(penId);
            log.info("查询结果: size={}", list.size());
            return R.OK(list);
        } catch (BusinessException e) {
            log.error("查询失败", e);
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "条件查询围栏列表")
    @PostMapping("/list")
    public R getListByCondition(@Validated @RequestBody FenceQueryDTO query) {
        // 添加日志
        log.info("接收到查询参数: {}", query);
        try {
            Page<FenceVO> page = fenceService.getListByCondition(query);
            
            // 构造返回的分页数据
            PageVO<FenceVO> result = new PageVO<>();
            result.setCurrent(page.getCurrent());
            result.setSize(page.getSize());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
            result.setRecords(page.getRecords());
            
            return R.OK(result);
        } catch (BusinessException e) {
            log.error("查询失败", e);
            return R.FAIL(e.getMessage());
        }
    }
}
